package com.yixun.qfbao.api.service.impl;

import com.yixun.qfbao.api.service.DataPermissionService;
import com.yixun.qfbao.api.service.DeptPermissionService;
import com.yixun.qfbao.api.service.PremisesPermissionService;
import com.yixun.qfbao.apidto.DeptPermissionBindDto;
import com.yixun.qfbao.apidto.SetDeptPermissionReqDto;
import com.yixun.qfbao.constant.RedisKeyConstant;
import com.yixun.qfbao.mapper.ChannelRoleMapper;
import com.yixun.qfbao.model.ChannelRole;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.model.dto.ChannelRoleDto;
import com.yixun.qfbao.model.dto.DeptPermissionDto;
import com.yixun.qfbao.model.dto.PartnerInfoDto;
import com.yixun.qfbao.model.enums.PermissionTypeEnum;
import com.yixun.qfbao.service.PartnerInfoService;
import com.yixun.qfbao.util.RedisUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class DataPermissionServiceImpl implements DataPermissionService {

    @Autowired
    private DeptPermissionService deptPermissionService;
    @Autowired
    private PremisesPermissionService premisesPermissionService;
    @Autowired
    private PartnerInfoService partnerInfoService;
    @Autowired
    private ChannelRoleMapper channelRoleMapper;

    @Override
    public DeptPermissionBindDto getBindDeptPermission(DeptPermissionDto entity) {
        return deptPermissionService.getBindDeptPermission(entity);
    }

    @Override
    public void setDeptPermission(SetDeptPermissionReqDto entity) {
        deptPermissionService.setDeptPermission(entity);
        if(Objects.nonNull(entity.getBelongsId())){
            cleanCompanyCacheByPartner(entity.getBelongsId());
        }
    }

    @Override
    public void setAgencyDeptPermission(SetDeptPermissionReqDto entity) {
        deptPermissionService.setAgencyDeptPermission(entity);
    }

    @Override
    public void resetDeptPermission(Long belongsId) {
        deptPermissionService.resetDeptPermission(belongsId);
    }

    @Override
    public Map<String, List<Long>> getPartnerPermission(Long partnerId, String permissionType) {
        Map<String, List<Long>> ppMap = new HashMap<>();
        ppMap.put(PermissionTypeEnum.PREMISES.getNo(), null);
        ppMap.put(PermissionTypeEnum.AGENT_ORG.getNo(), null);
        if(StringUtils.isBlank(permissionType) || PermissionTypeEnum.PREMISES.getNo().equals(permissionType)){
            List<Long> premisesPermission = premisesPermissionService.getPartnerPremisesPermission(partnerId);
            ppMap.put(PermissionTypeEnum.PREMISES.getNo(), premisesPermission);
        }else if(StringUtils.isBlank(permissionType) || PermissionTypeEnum.AGENT_ORG.getNo().equals(permissionType)){
            List<Long> agentOrgPermission = deptPermissionService.getPartnerDeptPermission(partnerId);
            ppMap.put(PermissionTypeEnum.AGENT_ORG.getNo(), agentOrgPermission);
        }
        return ppMap;
    }

    public void cleanCompanyCacheByPartner(Long partnerId){
        if(Objects.isNull(partnerId)){
            return;
        }
        PartnerInfo partnerInfo = partnerInfoService.selectById(partnerId);
        if(Objects.nonNull(partnerInfo)){
            PartnerInfoDto partnerInfoDto = new PartnerInfoDto();
            partnerInfoDto.setHigherid(partnerInfo.getHigherid());
            List<PartnerInfo> partnerInfoList = partnerInfoService.selectListByDto(partnerInfoDto);
            List<String> deleteKeys = new ArrayList<>();
            if(CollectionUtils.isNotEmpty(partnerInfoList)){
                for(PartnerInfo pInfo: partnerInfoList){
                    String userOrgPermissionKey = RedisKeyConstant.Permission.DEV_USER_ORG_PERMISSION + pInfo.getId();
                    String userPremisesPermissionKey = RedisKeyConstant.Permission.DEV_USER_PREMISES_PERMISSION + pInfo.getId();
                    deleteKeys.add(userOrgPermissionKey);
                    deleteKeys.add(userPremisesPermissionKey);
                }
            }
            ChannelRoleDto roleDto = new ChannelRoleDto();
        	roleDto.setMerchantId(partnerInfo.getHigherid());
        	List<ChannelRole> channelRoleList = channelRoleMapper.selectListByDto(roleDto);
        	channelRoleList.stream().forEach(channelRole -> {
        		String rolePremisesPermissionKey = RedisKeyConstant.Permission.DEV_ROLE_PREMISES_PERMISSION + channelRole.getId();
        		String roleOrgPermissionKey = RedisKeyConstant.Permission.DEV_ROLE_ORG_PERMISSION + channelRole.getId();
        		deleteKeys.add(rolePremisesPermissionKey);
                deleteKeys.add(roleOrgPermissionKey);
        	});
            RedisUtil.delete(deleteKeys);
        }
    }
}
